/**
 * JDBM LICENSE v1.00
 *
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright
 *    statements and notices.  Redistributions must also contain a
 *    copy of this document.
 *
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. The name "JDBM" must not be used to endorse or promote
 *    products derived from this Software without prior written
 *    permission of Cees de Groot.  For written permission,
 *    please contact cg@cdegroot.com.
 *
 * 4. Products derived from this Software may not be called "JDBM"
 *    nor may "JDBM" appear in their names without prior written
 *    permission of Cees de Groot.
 *
 * 5. Due credit should be given to the JDBM Project
 *    (http://jdbm.sourceforge.net/).
 *
 * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
 * Copyright 2000-2001 (C) Alex Boisvert. All Rights Reserved.
 * Contributions are Copyright (C) 2000 by their associated contributors.
 *
 * $Id: BaseRecordManager.java,v 1.8 2005/06/25 23:12:32 doomdark Exp $
 */

package com.sunchangming.stmdb.recman;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.sunchangming.stmdb.RecordManager;
import com.sunchangming.stmdb.helper.NameDirectorySer;
import com.sunchangming.stmdb.helper.Serializer;

public final class BaseRecordManager implements RecordManager {

	/**
	 * Underlying record file.
	 */
	private RecordFile _file;

	/**
	 * Physical row identifier manager.
	 */
	private final PhysicalRowIdManager _physMgr;

	/**
	 * Logigal to Physical row identifier manager.
	 */
	private final LogicalRowIdManager _logMgr;

	/**
	 * Page manager.
	 */
	private PageManager _pageman;

	/**
	 * Reserved slot for name directory.
	 */
	public static final int NAME_DIRECTORY_ROOT = 0;

	/**
	 * Static debugging flag
	 */
	public static final boolean DEBUG = false;

	/**
	 * Directory of named JDBMHashtables. This directory is a persistent
	 * directory, stored as a Hashtable. It can be retrived by using the
	 * NAME_DIRECTORY_ROOT.
	 */
	private Map<String, Long> _nameDirectory;

	/**
	 * Creates a record manager for the indicated file
	 * 
	 * @throws IOException
	 *             when the file cannot be opened or is not a valid file
	 *             content-wise.
	 */
	public BaseRecordManager(String filename) throws IOException {
		_file = new RecordFile(filename);
		_pageman = new PageManager(_file);
		_physMgr = new PhysicalRowIdManager(_file, _pageman);
		_logMgr = new LogicalRowIdManager(_file, _pageman);
	}

	/**
	 * Get the underlying Transaction Manager
	 */
	public synchronized TransactionManager getTransactionManager() {
		checkIfClosed();

		return _file.txnMgr;
	}

	/**
	 * Closes the record manager.
	 * 
	 * @throws IOException
	 *             when one of the underlying I/O operations fails.
	 */
	@Override
	public synchronized void close() throws IOException {
		checkIfClosed();

		_pageman.close();
		_pageman = null;

		_file.close();
		_file = null;
	}

	/**
	 * Inserts a new record using a custom serializer.
	 * 
	 * @param obj
	 *            the object for the new record.
	 * @param serializer
	 *            a custom serializer
	 * @return the rowid for the new record.
	 * @throws IOException
	 *             when one of the underlying I/O operations fails.
	 */
	@Override
	public synchronized long insert(Object obj, Serializer serializer) throws IOException {
		byte[] data;
		long recid;
		Location physRowId;

		checkIfClosed();

		data = serializer.serialize(obj);
		physRowId = _physMgr.insert(data, 0, data.length);
		recid = _logMgr.insert(physRowId).toLong();
		if (DEBUG) {
			System.out.println("BaseRecordManager.insert() recid " + recid + " length " + data.length);
		}
		return recid;
	}

	/**
	 * Deletes a record.
	 * 
	 * @param recid
	 *            the rowid for the record that should be deleted.
	 * @throws IOException
	 *             when one of the underlying I/O operations fails.
	 */
	@Override
	public synchronized void delete(long recid) throws IOException {
		checkIfClosed();
		if (recid <= 0) {
			throw new IllegalArgumentException("Argument 'recid' is invalid: " + recid);
		}

		if (DEBUG) {
			System.out.println("BaseRecordManager.delete() recid " + recid);
		}

		final Location logRowId = new Location(recid);
		final Location physRowId = _logMgr.fetch(logRowId);
		_physMgr.delete(physRowId);
		_logMgr.delete(logRowId);
	}

	/**
	 * Updates a record using a custom serializer.
	 * 
	 * @param recid
	 *            the recid for the record that is to be updated.
	 * @param obj
	 *            the new object for the record.
	 * @param serializer
	 *            a custom serializer
	 * @throws IOException
	 *             when one of the underlying I/O operations fails.
	 */
	@Override
	public synchronized void update(long recid, Object obj, Serializer serializer) throws IOException {
		checkIfClosed();
		if (recid <= 0) {
			throw new IllegalArgumentException("Argument 'recid' is invalid: " + recid);
		}

		final Location logRecid = new Location(recid);
		final Location physRecid = _logMgr.fetch(logRecid);

		final byte[] data = serializer.serialize(obj);
		if (DEBUG) {
			System.out.println("BaseRecordManager.update() recid " + recid + " length " + data.length);
		}

		final Location newRecid = _physMgr.update(physRecid, data, 0, data.length);
		if (!newRecid.equals(physRecid)) {
			_logMgr.update(logRecid, newRecid);
		}
	}

	/**
	 * Fetches a record using a custom serializer.
	 * 
	 * @param recid
	 *            the recid for the record that must be fetched.
	 * @param serializer
	 *            a custom serializer
	 * @return the object contained in the record.
	 * @throws IOException
	 *             when one of the underlying I/O operations fails.
	 */
	@Override
	public synchronized Object fetch(long recid, Serializer serializer) throws IOException {
		byte[] data;

		checkIfClosed();
		if (recid <= 0) {
			throw new IllegalArgumentException("Argument 'recid' is invalid: " + recid);
		}
		data = _physMgr.fetch(_logMgr.fetch(new Location(recid)));
		if (DEBUG) {
			System.out.println("BaseRecordManager.fetch() recid " + recid + " length " + data.length);
		}
		return serializer.deserialize(data);
	}

	/**
	 * Returns the number of slots available for "root" rowids. These slots can
	 * be used to store special rowids, like rowids that point to other rowids.
	 * Root rowids are useful for bootstrapping access to a set of data.
	 */
	@Override
	public int getRootCount() {
		return FileHeader.NROOTS;
	}

	/**
	 * Returns the indicated root rowid.
	 * 
	 * @see #getRootCount
	 */
	@Override
	public synchronized long getRoot(int id) throws IOException {
		checkIfClosed();

		return _pageman.getFileHeader().getRoot(id);
	}

	/**
	 * Sets the indicated root rowid.
	 * 
	 * @see #getRootCount
	 */
	@Override
	public synchronized void setRoot(int id, long rowid) throws IOException {
		checkIfClosed();

		_pageman.getFileHeader().setRoot(id, rowid);
	}

	/**
	 * Obtain the record id of a named object. Returns 0 if named object doesn't
	 * exist.
	 */
	@Override
	public long getNamedObject(String name) throws IOException {
		checkIfClosed();

		final Map<String, Long> nameDirectory = getNameDirectory();
		final Long recid = nameDirectory.get(name);
		if (recid == null) {
			return 0;
		}
		return recid.longValue();
	}

	/**
	 * Set the record id of a named object.
	 */
	@Override
	public void setNamedObject(String name, long recid) throws IOException {
		checkIfClosed();

		final Map<String, Long> nameDirectory = getNameDirectory();
		if (recid == 0) {
			// remove from hashtable
			nameDirectory.remove(name);
		} else {
			nameDirectory.put(name, Long.valueOf(recid));
		}
		saveNameDirectory(nameDirectory);
	}

	/**
	 * Commit (make persistent) all changes since beginning of transaction.
	 */
	@Override
	public synchronized void commit() throws IOException {
		checkIfClosed();

		_pageman.commit();
	}

	/**
	 * Rollback (cancel) all changes since beginning of transaction.
	 */
	@Override
	public synchronized void rollback() throws IOException {
		checkIfClosed();

		_pageman.rollback();
	}

	/**
	 * Load name directory
	 */
	@SuppressWarnings("unchecked")
	private Map<String, Long> getNameDirectory() throws IOException {
		// retrieve directory of named hashtable
		long nameDirectory_recid = getRoot(NAME_DIRECTORY_ROOT);
		if (nameDirectory_recid == 0) {
			_nameDirectory = new HashMap<String, Long>();
			nameDirectory_recid = insert(_nameDirectory, new NameDirectorySer());
			setRoot(NAME_DIRECTORY_ROOT, nameDirectory_recid);
		} else {
			_nameDirectory = (Map<String, Long>) fetch(nameDirectory_recid, new NameDirectorySer());
		}
		return _nameDirectory;
	}

	private void saveNameDirectory(Map<String, Long> directory) throws IOException {
		final long recid = getRoot(NAME_DIRECTORY_ROOT);
		if (recid == 0) {
			throw new IOException("Name directory must exist");
		}
		update(recid, directory, new NameDirectorySer());
	}

	/**
	 * Check if RecordManager has been closed. If so, throw an
	 * IllegalStateException.
	 */
	private void checkIfClosed() throws IllegalStateException {
		if (_file == null) {
			throw new IllegalStateException("RecordManager has been closed");
		}
	}
}
